home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Dr. Windows 3
/
dr win3.zip
/
dr win3
/
NEW_TECH
/
TUSRC.ZIP
/
SRC
/
SPLIT.C
< prev
next >
Wrap
C/C++ Source or Header
|
1993-10-02
|
13KB
|
557 lines
/* split.c -- split a file into pieces.
Copyright (C) 1988, 1991 Free Software Foundation, Inc.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2, or (at your option)
any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. */
/* By tege@sics.se, with rms.
To do:
* Implement -t CHAR or -t REGEX to specify break characters other
than newline. */
#include <stdio.h>
#include <io.h>
#include "../lib/getopt.h"
#include <sys/types.h>
#include "system.h"
#include "version.h"
char *xmalloc ();
void error ();
static int convint ();
static int isdigits ();
static int stdread ();
static void line_bytes_split ();
static void bytes_split ();
static void cwrite ();
static void lines_split ();
static void next_file_name ();
/* The name this program was run with. */
char *program_name;
/* Base name of output files. */
static char *outfile;
/* Pointer to the end of the prefix in OUTFILE.
Suffixes are inserted here. */
static char *outfile_mid;
/* Pointer to the end of OUTFILE. */
static char *outfile_end;
/* Status for outfile name generation. */
static unsigned outfile_count = (unsigned) -1;
static unsigned outfile_name_limit = 25 * 26;
static unsigned outfile_name_generation = 1;
/* Name of input file. May be "-". */
static char *infile;
/* Descriptor on which input file is open. */
static int input_desc;
/* Descriptor on which output file is open. */
static int output_desc;
/* If non-zero, display usage information and exit. */
static int flag_help;
/* If non-zero, print the version on standard error. */
static int flag_version;
static struct option const longopts[] =
{
{"bytes", required_argument, NULL, 'b'},
{"lines", required_argument, NULL, 'l'},
{"line-bytes", required_argument, NULL, 'C'},
{"help", no_argument, &flag_help, 1},
{"version", no_argument, &flag_version, 1},
{NULL, 0, NULL, 0}
};
static void
usage (reason)
char *reason;
{
if (reason != NULL)
fprintf (stderr, "%s: %s\n", program_name, reason);
fprintf (stderr, "\
Usage: %s [-lines] [-l lines] [-b bytes[bkm]] [-C bytes[bkm]]\n\
[--lines=lines] [--bytes=bytes[bkm]] [--line-bytes=bytes[bkm]]\n\
[--help] [--version] [infile [outfile-prefix]]\n",
program_name);
exit (2);
}
void
main (argc, argv)
int argc;
char *argv[];
{
struct stat stat_buf;
int num; /* numeric argument from command line */
enum
{
type_undef, type_bytes, type_byteslines, type_lines, type_digits
} split_type = type_undef;
int in_blk_size; /* optimal block size of input file device */
char *buf; /* file i/o buffer */
int accum = 0;
char *outbase;
int c;
int digits_optind = 0;
program_name = argv[0];
/* Parse command line options. */
infile = "-";
outbase = "x";
while (1)
{
/* This is the argv-index of the option we will read next. */
int this_optind = optind ? optind : 1;
c = getopt_long (argc, argv, "0123456789b:l:C:", longopts, (int *) 0);
if (c == EOF)
break;
switch (c)
{
case 0:
break;
case 'b':
if (split_type != type_undef)
usage ("cannot split in more than one way");
split_type = type_bytes;
if (convint (optarg, &accum) == -1)
usage ("invalid number of bytes");
break;
case 'l':
if (split_type != type_undef)
usage ("cannot split in more than one way");
split_type = type_lines;
if (!isdigits (optarg))
usage ("invalid number of lines");
accum = atoi (optarg);
break;
case 'C':
if (split_type != type_undef)
usage ("cannot split in more than one way");
split_type = type_byteslines;
if (convint (optarg, &accum) == -1)
usage ("invalid number of bytes");
break;
case '0':
case '1':
case '2':
case '3':
case '4':
case '5':
case '6':
case '7':
case '8':
case '9':
if (split_type != type_undef && split_type != type_digits)
usage ("cannot split in more than one way");
if (digits_optind != 0 && digits_optind != this_optind)
accum = 0; /* More than one number given; ignore other. */
digits_optind = this_optind;
split_type = type_digits;
accum = accum * 10 + c - '0';
break;
default:
usage ((char *)0);
}
}
if (flag_version)
{
fprintf (stderr, "%s\n", version_string);
exit (0);
}
if (flag_help)
usage ((char *)0);
/* Handle default case. */
if (split_type == type_undef)
{
split_type = type_lines;
accum = 1000;
}
if (accum < 1)
usage ("invalid number");
num = accum;
/* Get out the filename arguments. */
if (optind < argc)
infile = argv[optind++];
if (optind < argc)
outbase = argv[optind++];
if (optind < argc)
usage ("too many arguments");
/* Open the input file. */
if (!strcmp (infile, "-"))
input_desc = 0;
else
{
input_desc = open (infile, O_RDONLY);
if (input_desc < 0)
error (1, errno, "%s", infile);
}
/* No output file is open now. */
output_desc = -1;
/* Copy the output file prefix so we can add suffixes to it.
26**29 is certainly enough output files! */
outfile = xmalloc (strlen (outbase) + 30);
strcpy (outfile, outbase);
outfile_mid = outfile + strlen (outfile);
outfile_end = outfile_mid + 2;
bzero (outfile_mid, 30);
outfile_mid[0] = 'a';
outfile_mid[1] = 'a' - 1; /* first call to next_file_name makes it an 'a' */
/* Get the optimal block size of input device and make a buffer. */
if (fstat (input_desc, &stat_buf) < 0)
error (1, errno, "%s", infile);
in_blk_size = ST_BLKSIZE (stat_buf);
buf = xmalloc (in_blk_size + 1);
switch (split_type)
{
case type_digits:
case type_lines:
lines_split (num, buf, in_blk_size);
break;
case type_bytes:
bytes_split (num, buf, in_blk_size);
break;
case type_byteslines:
line_bytes_split (num);
break;
default:
abort ();
}
if (close (input_desc) < 0)
error (1, errno, "%s", infile);
if (output_desc >= 0 && close (output_desc) < 0)
error (1, errno, "%s", outfile);
exit (0);
}
/* Return nonzero if the string STR is composed entirely of decimal digits. */
static int
isdigits (str)
char *str;
{
do
{
if (!ISDIGIT (*str))
return 0;
str++;
}
while (*str);
return 1;
}
/* Put the value of the number in STR into *VAL.
STR can specify a positive integer, optionally ending in `k'
to mean kilo or `m' to mean mega.
Return 0 if STR is valid, -1 if not. */
static int
convint (str, val)
char *str;
int *val;
{
int multiplier = 1;
int arglen = strlen (str);
if (arglen > 1)
{
switch (str[arglen - 1])
{
case 'b':
multiplier = 512;
str[arglen - 1] = '\0';
break;
case 'k':
multiplier = 1024;
str[arglen - 1] = '\0';
break;
case 'm':
multiplier = 1048576;
str[arglen - 1] = '\0';
break;
}
}
if (!isdigits (str))
return -1;
*val = atoi (str) * multiplier;
return 0;
}
/* Split into pieces of exactly NCHARS bytes.
Use buffer BUF, whose size is BUFSIZE. */
static void
bytes_split (nchars, buf, bufsize)
int nchars;
char *buf;
int bufsize;
{
int n_read;
int new_file_flag = 1;
int to_read;
int to_write = nchars;
char *bp_out;
do
{
n_r